package org.fujay.langchain4jserver.interfaces.api;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.fujay.commons.core.result.R;
import org.fujay.langchain4jserver.application.command.CreateModelCommand;
import org.fujay.langchain4jserver.application.command.UpdateModelCommand;
import org.fujay.langchain4jserver.application.dto.ModelConnectionTestResultDTO;
import org.fujay.langchain4jserver.application.dto.ModelDTO;
import org.fujay.langchain4jserver.application.dto.ModelSummaryDTO;
import org.fujay.langchain4jserver.application.service.ModelService;
import org.fujay.langchain4jserver.domain.model.vo.ModelId;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * 模型管理 API
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/models")
@RequiredArgsConstructor
public class ModelController {

    private final ModelService modelService;

    /**
     * 创建新模型
     */
    @PostMapping
    public R<ModelDTO> createModel(@Valid @RequestBody CreateModelCommand command) {
        ModelDTO model = modelService.createModel(command);
        return R.ok(model);
    }

    /**
     * 获取模型列表
     */
    @GetMapping
    public R<List<ModelSummaryDTO>> listModels() {
        return R.ok(modelService.listModels());
    }

    /**
     * 获取模型详情
     */
    @GetMapping("/{id}")
    public R<ModelDTO> getModelDetail(@PathVariable Long id) {
        ModelId modelId = new ModelId(id);
        ModelDTO model =
                modelService
                        .getModel(modelId)
                        .orElseThrow(() -> new NoSuchElementException("Model not found: " + id));
        return R.ok(model);
    }

    /**
     * 更新模型信息与配置
     */
    @PutMapping
    public R<ModelDTO> updateModel(@Valid @RequestBody UpdateModelCommand request) {
        return R.ok(
                modelService
                        .updateModel(request)
                        .orElseThrow(() -> new NoSuchElementException("Model not found")));
    }

    /**
     * 删除模型
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteModel(@PathVariable Long id) {
        ModelId modelId = new ModelId(id);
        modelService.deleteModel(modelId);
        return R.ok();
    }

    /**
     * 启用模型
     */
    @PostMapping("/{id}/enable")
    public R<ModelDTO> enableModel(@PathVariable Long id) {
        ModelId modelId = new ModelId(id);
        return R.ok(
                modelService
                        .enableModel(modelId)
                        .orElseThrow(() -> new NoSuchElementException("Model not found: " + id)));
    }

    /**
     * 禁用模型
     */
    @PostMapping("/{id}/disable")
    public R<ModelDTO> disableModel(@PathVariable Long id) {
        ModelId modelId = new ModelId(id);
        return R.ok(
                modelService
                        .disableModel(modelId)
                        .orElseThrow(() -> new NoSuchElementException("Model not found: " + id)));
    }

    /**
     * 测试模型连接
     */
    @PostMapping("/{id}/test-connection")
    public R<ModelConnectionTestResultDTO> testModelConnection(@PathVariable Long id) {
        ModelId modelId = new ModelId(id);
        ModelConnectionTestResultDTO result = modelService.testModelConnection(modelId);
        return R.ok(result);
    }
}
